package com.taxi.driver.widget;

import android.content.Context;
import android.os.Handler;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taxi.driver.R;

public class XScrollView extends NestedScrollView {
    private XScrollViewListener listener;
    private View foodView;
    private TextView hint;
    private boolean isMeasure;
    private Handler handler = new Handler();
    private final int DELAY = 800;

    public XScrollView(Context context) {
        this(context, null);
    }

    public XScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public XScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setFocusable(false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (!isMeasure) {
            isMeasure = true;
            addFoodView();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                int y = getScrollY();
                if (y <= 0) {
                    //滚到顶部
                } else if (getChildAt(0).getMeasuredHeight() <= y + getHeight()) {
                    //滚到底部
                    if (foodView != null) {
                        foodView.setVisibility(VISIBLE);
                        hint.setText(getContext().getResources().getString(R.string.loading));
                    }

                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // 追加内容
                            if (listener != null)
                                listener.onLoadMore();
                        }
                    }, DELAY);
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    public interface XScrollViewListener {
        void onLoadMore();
    }

    public void setXScrollViewListener(XScrollViewListener listener) {
        this.listener = listener;
    }

    public void addFoodView() {
        foodView = LayoutInflater.from(getContext()).inflate(R.layout.view_xscrollview_footer, null);
        foodView.setVisibility(GONE);
        hint = (TextView) foodView.findViewById(R.id.hint);
        ViewGroup viewGroup = (ViewGroup) getChildAt(0);
        viewGroup.addView(foodView, viewGroup.getChildCount());
    }

    public void setPullLoadEnable(boolean isEnable) {
        if (isEnable && foodView != null) {
            foodView.setVisibility(VISIBLE);
            hint.setText(getContext().getResources().getString(R.string.loading));
        } else {
            foodView.setVisibility(GONE);
        }
    }

    public void stopLoadMore() {
        if (foodView != null)
            foodView.setVisibility(GONE);
    }

    /**
     * 显示“没有更多”的提示
     */
    public void showNoMoreHint() {
        if (foodView != null) {
            foodView.setVisibility(VISIBLE);
            hint.setText(getContext().getResources().getString(R.string.no_more));
        }
    }
}
